package com.qyhl.webtv.commonlib.item;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.Map;

/**
 * Created by qyhl on 2018/1/31.
 */

public class ItemTeleText implements ItemViewDelegate<GlobalNewsBean> {
    private Context context;

    public ItemTeleText(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.news_item_newslist_teletext;
    }

    @Override
    public boolean isForViewType(GlobalNewsBean item, int position) {
        return "14".equals(item.getType());
    }

    @Override
    public void convert(ViewHolder holder, GlobalNewsBean bean, int position) {
        TextView title = holder.getView(R.id.title);
        title.setText(bean.getTitle());

        title.setTextColor(context.getResources().getColor(R.color.global_black_lv1));
        if (CommonUtils.getInstance().getHasHistory()) {
            if (Hawk.contains("readHistory")) {
                Map<String, String> historyMap = CommonUtils.getInstance().getReadHistory();
                for (Map.Entry<String, String> vo : historyMap.entrySet()) {
                    String newsId = (String) vo.getKey();
                    if (bean.getNewsId().equals(newsId)) {
                        title.setTextColor(context.getResources().getColor(R.color.global_gray_lv1));
                        break;
                    }
                }
            }
        }

        RoundedImageView cover = holder.getView(R.id.cover);
        Glide.with(context)
                .load(bean.getCover())
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(cover);

        TextView status = holder.getView(R.id.status);

        TextView startTime = holder.getView(R.id.start_time);

        switch (bean.getOrigin()) {
            case "2":
                status.setText("直播");
                status.setVisibility(View.VISIBLE);
                startTime.setVisibility(View.GONE);
                break;
            case "7":
                status.setText("回顾");
                status.setVisibility(View.VISIBLE);
                startTime.setVisibility(View.GONE);
                break;
            case "6":
                status.setText("预告");
                status.setVisibility(View.VISIBLE);
                startTime.setVisibility(View.VISIBLE);
                startTime.setText(DateUtils.foreShowTime(bean.getPublishDate()));
                startTime.setVisibility(View.GONE);
                break;
            default:
                status.setVisibility(View.GONE);
                startTime.setVisibility(View.GONE);
                break;
        }


        TextView scan = holder.getView(R.id.scan_num);
        try {
            int minPlay = Integer.parseInt(CommonUtils.getInstance().getMinPlay());
            if (minPlay == 0) {
                scan.setVisibility(View.GONE);
            } else {
                //判断评论数是否大于最少浏览数
                if (StringUtils.isEmpty(bean.getScanNum() + "")) {
                    scan.setVisibility(View.GONE);
                } else if (bean.getScanNum() >=
                        minPlay) {
                    scan.setText(StringUtils.transformNum(bean.getScanNum() + "") + " 人参与");
                    scan.setVisibility(View.VISIBLE);
                } else {
                    scan.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            scan.setVisibility(View.GONE);
        }


    }
}
